package com.zangs.emqx_expand.web.controller.emqx;


import com.zangs.emqx_expand.common.annotation.SignatureApi;
import com.zangs.emqx_expand.common.core.controller.BaseController;
import com.zangs.emqx_expand.emqx.domain.*;
import javax.annotation.Resource;

import com.zangs.emqx_expand.emqx.service.MQTTService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/auth")
public class MqttAuthController extends BaseController {
    @Resource
    private MQTTService mqttService;

    /**
     * 校验登陆
     *
     * @param emqxLoginUser 需要校验的登陆信息
     */
    @SignatureApi(signature = false,remark = "emqx调用此接口进行登录")
    @PostMapping("/login")
    public ResponseEntity<?> login(
            @RequestBody EmqxLoginUser emqxLoginUser) {

        return mqttService.login(emqxLoginUser);
    }

    /**
     * 校验权限
     *
     * @param emqxAcl 需要校验的规则信息
     */
    @SignatureApi(signature = false,remark = "emqx调用此接口查询是否有权限")
    @PostMapping("/hasAcl")
    public ResponseEntity<?> acl(@RequestBody EmqxAcl emqxAcl) {
        Map<String, String> responseBody = new HashMap<>();
        if (!mqttService.hasAcl(emqxAcl)) {
            responseBody.put("result", "deny");
        } else {
            responseBody.put("result", "allow");
        }
        return ResponseEntity.status(HttpStatus.OK).header("Content-Type", "application/json").body(responseBody);
    }




}
